﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.Models;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class VerticalSlider : UserControl
    {
        #region Private Variables

            private double barInitial;
            private double barDistance;

            private double thumbWidth;

            private double maxValue;
            private double minValue;

            private double percent;

            private MouseManager mouseEventManager;

        #endregion

        #region Constructors

            public VerticalSlider() :
                this(201.828, 0, 100)
            {
            }

            /// <summary>
            /// Creates a vertical slider control
            /// </summary>
            /// <param name="sliderBarWidth">Width of the slider</param>
            /// <param name="sliderMinValue">Max value represented by the slider</param>
            /// <param name="sliderMaxValue">Min value represented by the slider</param>
            public VerticalSlider(double sliderBarWidth, double sliderMinValue, double sliderMaxValue)
            {
                // Required to initialize variables
                InitializeComponent();
                
                // Initialize Variables                
                barInitial = (double)SliderBar.GetValue(Canvas.TopProperty);
                barDistance = sliderBarWidth;
                minValue = sliderMinValue;
                maxValue = sliderMaxValue;                
                thumbWidth = SliderThumb.Width;
                SliderBar.Height = sliderBarWidth;

                mouseEventManager = new MouseManager("vertical", barInitial, barDistance, thumbWidth);

                // Hook up event handlers
                SliderThumb.MouseEnter += mouseEventManager.OnMouseEnterHand;
                SliderThumb.MouseLeave += mouseEventManager.OnMouseLeaveArrow;
                SliderThumb.MouseLeftButtonDown += mouseEventManager.SliderMouseDown;
                SliderThumb.MouseLeftButtonUp += mouseEventManager.SliderMouseUp;
                SliderThumb.MouseMove += mouseEventManager.SliderMouseMove;

                mouseEventManager.sliderMove += new MouseManager.SliderMoveEventHandler(OnSliderThumbMove);
            }

        #endregion

        #region Public Methods

            /// <summary>
            /// Get the left position of the slider bar
            /// </summary>
            /// <returns></returns>
            public double GetSliderBarLeft()
            {
                return barInitial;
            }

            /// <summary>
            /// Get the width of the slider bar
            /// </summary>
            /// <returns></returns>
            public double GetSliderBarWidth()
            {
                return barDistance;
            }

            /// <summary>
            /// Get the width of the slider thumb
            /// </summary>
            /// <returns></returns>
            public double GetSliderThumbWidth()
            {
                return thumbWidth;
            }

            /// <summary>
            /// Get the distance from the left of 
            /// the slider thumb as a percentage
            /// </summary>
            /// <returns></returns>
            public double GetPercentage()
            {
                return percent;
            }

            /// <summary>
            /// Sets the slider thumb position on mouse move.
            /// Also raises event sliderMoved;
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void OnSliderThumbMove(object sender, DiscoveryLogic.UI.Layouts.Models.CustomEventArgs e)
            {            
                SliderThumb.SetValue(Canvas.TopProperty, e.SliderThumbValue);

                this.percent = ((e.SliderThumbValue + thumbWidth / 2) - barInitial) / barDistance;

                if (sliderMoved != null)
                {
                    sliderMoved(this, e);
                }
            }

        #endregion

        #region Events

            public event EventHandler sliderMoved;

        #endregion
    }
}
